<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="日程进行时间" prop="proTime">
        <el-date-picker clearable
          v-model="queryParams.proTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日程进行时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="员工姓名" prop="empId"  clearable
          @keyup.enter.native="handleQuery">
        <!-- <el-input
          v-model="queryParams.empId"
          placeholder="请输入员工编号"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.empId" placeholder="请选择员工姓名">
              <el-option
                v-for="item in options"
                :key="item.empId"
                :label="item.empName"
                :value="item.empId">
              </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="日程状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择日程状态" clearable
          @keyup.enter.native="handleQuery">
              <el-option
                 v-for="item in status1"
                 :key="item.status"
                :label="item.statusName"
                :value="item.status">
              </el-option>
            </el-select>

      </el-form-item>
       <el-form-item label="截至时间" prop="effectiveTimeMin">
          
    <el-date-picker
      v-model="queryParams.effectiveTimeMin"
      type="date"
      value-format="yyyy-MM-dd"
      placeholder="选择开始时间" 
      clearable
      @keyup.enter.native="handleQuery">
    </el-date-picker>
       
      </el-form-item>-
       <el-form-item  prop="effectiveTimeMax">

<el-date-picker
          clearable
      v-model="queryParams.effectiveTimeMax"
      value-format="yyyy-MM-dd"
      type="date"
      placeholder="选择结束时间"  @keyup.enter.native="handleQuery">
    </el-date-picker>

      </el-form-item> 
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" round @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" round @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          round
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['schedule:programme:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          round
          icon="el-icon-edit"
          size="small"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['schedule:programme:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
         round
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['schedule:programme:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
         round
          icon="el-icon-download"
          size="small"
          @click="handleExport"
          v-hasPermi="['schedule:programme:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          round
          icon="el-icon-refresh-right"
          size="small"
          @click="handleRollback"
          v-hasPermi="['personnel:employee:export']"
        >回滾</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="programmeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="编号" align="center" prop="proId" /> -->

      <el-table-column label="日程内容" align="center" prop="content" />
      <el-table-column label="日程状态" align="center" prop="status" >
        <!--        自定义标签-->
        <template slot-scope="scope">
          <el-tag  v-if="scope.row.status == 0">已完成</el-tag>
          <el-tag  type="danger" v-if="scope.row.status == 1">未完成</el-tag>
          <el-tag type="warning" v-if="scope.row.status == 2">进行中</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="日程进行时间" align="center" prop="proTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.proTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="员工姓名" align="center" prop="empName" />
         <el-table-column label="任务进度" align="center">
          <template slot-scope="scope">
              <el-progress :percentage="scope.row.status == 1 ? 0 : (scope.row.status == 0 ? 100 : 50)">
              </el-progress>
          </template>
      </el-table-column>
      <el-table-column label="截至时间" align="center" prop="isBlack" width="180">
        <template slot-scope="scope">
          {{scope.row.deadline==null?'无截至时间':scope.row.deadline}}<br>
          <el-tag v-show="scope.row.status != 0" type="danger" v-if="scope.row.isBlack != '已逾期0天' && scope.row.isBlack != null ">{{scope.row.isBlack}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            round
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['schedule:programme:edit']"
          >修改</el-button>
          <el-button
            size="small"
            round
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['schedule:programme:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改日程信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日程内容" prop="content">
          <el-input type="textarea" v-model="form.content" :autosize="{ minRows: 3}" maxlength="60" show-word-limit/>
        </el-form-item>
        <el-form-item label="日程状态" prop="status">
          <template>
            <el-radio v-model="form.status" label="0">已完成</el-radio>
            <el-radio v-model="form.status" label="1">未完成</el-radio>
            <el-radio v-model="form.status" label="2">进行中</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="进行时间"  prop="proTime">
          <el-date-picker clearable
      v-model="form.proTime"
      :disabled="verification"
      align="right"
      type="date"
      value-format="yyyy-MM-dd"
      placeholder="选择日期"
      :picker-options="pickerOptions">
    </el-date-picker>
        </el-form-item>
        <el-form-item label="员工姓名" prop="empId">
          <el-select v-model="form.empId"  :disabled="verification" placeholder="请选择员工姓名">
              <el-option
                v-for="item in options"
                :key="item.empId"
                :label="item.empName"
                :value="item.empId">
              </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="截至时间" prop="deadline">
          <el-date-picker clearable
            v-model="form.deadline"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择截至时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" round @click="submitForm">确 定</el-button>
        <el-button size="small"
            round @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProgramme, getProgramme, delProgramme, addProgramme, updateProgramme,rollback } from "@/api/schedule/programme";
/**员工查询 */
import { listEmployee} from "@/api/personnel/employee";
import { errorMonitor } from 'events';
export default {
  name: "Programme",
  data() {
    return {
      //查询结算类型下拉框
      status1: [
        { status: 0, statusName: "已完成" },
        { status: 1, statusName: "未完成" },
         { status: 2, statusName: "进行中" },
      ],
      //修改添加时，禁用
      verification:false,
      //时间
       pickerOptions: {
          disabledDate(time) {
             return time.getTime() > Date.now();
            // return time.getTime() < Date.now()-(24 * 60 * 60 * 1000);
          }
          },
      progType:[
        {progTypeId:0,progTypeName:"公事"},
         {progTypeId:1,progTypeName:"私事"},
          {progTypeId:2,progTypeName:"其他"}
      ],
       options: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日程信息管理表格数据
      programmeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: null,
        status: null,
        proTime: null,
        empId: null,
        // progTypeId: null,
        version: null,
        deadline: null,
         effectiveTimeMin:null,
        effectiveTimeMax:null,
       
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" },
           {max: 59, message: "最大字符应小于60个", trigger: "change"}
        ],
        status:[
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        progTypeId:[
           { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        empId:[
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        proTime:[
          { required: true, message: "时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.listEmployee();
  },
  methods: {
    //进度条
    format(percentage) {
           return percentage === 100 ? '满' : `${percentage}%`;
      },
     /** 获取所有员工名称 */
    listEmployee() {
      this.loading = true;
      listEmployee().then(response => {
      this.options = response.rows

      });
    },
    /** 查询日程信息管理列表 */
    getList() {
      this.loading = true;
      listProgramme(this.queryParams).then(response => {
        this.programmeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        proId: null,
        content: null,
        status: null,
        proTime: null,
        empId: null,
        // progTypeId: null,
        createTime: null,
        updateTime: null,
        version: null,
        deadline: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.proId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.verification=false;
      this.reset();
      this.open = true;
      this.title = "添加日程信息管理";

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.verification=true;
      this.reset();
      const proId = row.proId || this.ids
      getProgramme(proId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改日程信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.proId != null) {
            updateProgramme(this.form).then(response => {
              if(response){
                this.$modal.msgSuccess("修改成功");
              this.open = false;
              //进度条
              this.format();
              this.getList();
              }else{
                this.$modal.msgError("截至时间必须大于进行时间");
              }
            });
          } else {
            addProgramme(this.form).then(response => {
             if (response) {
                 this.$modal.msgSuccess("新增成功");
              this.open = false;
               //进度条
              this.format();
              this.getList();
              }else{
              this.$modal.msgError("截至时间必须大于进行时间");
              }
            });
          }
        }
      });
    },
    //删除
    handleDelete(row) {
      const proIds = row.proId || this.ids;
      this.$modal.confirm('是否确认删除请假管理编号为"' + proIds + '"的数据项？').then(function() {
      }).then(() => {
         if(row.status==1 || row.status==2){
            this.$modal.msgError("状态处于未完成或进行中，删除失败");
          }else{
          let res = delProgramme(proIds);
             res.then((res)=>{
          if (res){
            this.getList();
            this.$modal.msgSuccess("删除成功");
          } else {
            this.$modal.msgError("未达到结束时间，删除失败");
          }
        })
          }

      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('schedule/programme/export', {
        ...this.queryParams
      }, `programme_${new Date().getTime()}.xlsx`)
    },
    /**回滚操作 */
     handleRollback(){
      rollback().then((res)=>{
        if (!res){
          this.$message({
            message: '目前不存在撤销功能',
            type: 'warning'
          })
        }else {
           this.$message({
            message: '回滚成功',
            type: 'success'
          })
          this.getList();
        }
      })
    }
  }
};
</script>
<style>
</style>

